package com.carey.myblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carey.myblog.pojo.BlogTags;
import com.carey.myblog.pojo.Tag;
import com.carey.myblog.service.BlogTagsService;
import com.carey.myblog.mapper.BlogTagsMapper;
import com.carey.myblog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author carey-
* @description 针对表【t_blog_tags】的数据库操作Service实现
* @createDate 2022-05-25 22:09:47
*/
@Service
public class BlogTagsServiceImpl extends ServiceImpl<BlogTagsMapper, BlogTags>
    implements BlogTagsService{

    @Autowired
    private TagService tagService;

    @Override
    public List<Tag> convertToList(String ids) { //将字符串转换为一个数组list
        List<Long> list = new ArrayList<>();
        if (!"".equals(ids) && ids != null) {
            String[] idarray = ids.split(",");
            for (int i=0; i < idarray.length;i++) {
                list.add(new Long(idarray[i]));
            }
        }
        List<Tag> tagList = new ArrayList<>();
        list.forEach(id ->{
            Tag tag = tagService.getById(id);
            tagList.add(tag);
        });
        return tagList;
    }

    @Override
    public void delete(Long id) {
        QueryWrapper<BlogTags> wrapper = new QueryWrapper<>();
        wrapper.eq("blogs_id",id);
        baseMapper.delete(wrapper);
    }

    @Override
    public void add(BlogTags blogTags) {
        baseMapper.insert(blogTags);
    }

    @Override
    public List<Tag> getTypeids(Long id) {
        QueryWrapper<BlogTags> wrapper = new QueryWrapper<>();
        wrapper.eq("blogs_id",id);
        List<Tag> list = new ArrayList<>();
        for (BlogTags blogTags : baseMapper.selectList(wrapper)) {
            list.add(tagService.getTag(blogTags.getTagsId()));
        }

        return list;
    }
}




